package com.webank.aigc.service.neo4j;

import jakarta.annotation.PostConstruct;
import org.neo4j.driver.*;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class Neo4jServiceImpl implements Neo4jService{
    final String dbUri = "neo4j+s://f52525e4.databases.neo4j.io";
    final String dbUser = "neo4j";
    final String dbPassword = "aLsrh1_NOO01JDeQhs2227sq0M4piswjUAZi7pdHn98";

    Driver driver;
    @PostConstruct
    void init(){
        try{
            driver = GraphDatabase.driver(dbUri, AuthTokens.basic(dbUser, dbPassword));
            driver.verifyConnectivity();
            System.out.println("Connection established.");
        }
        catch(Exception e){
            System.out.println("Connection error.");
        }
    }

    @Override
    public EagerResult query(String question) {
        EagerResult result;
        try {
            result = driver.executableQuery(question)
                    .withConfig(QueryConfig.builder().withDatabase("neo4j").build())
                    .execute();
        }
        catch (Exception e){
            System.out.println("execute cypher error");
            return null;
        }

        return result;
    }

}
